/**
 * 
 */
package com.solome.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 时间戳工具类
 * 
 * @author Sirius
 */
public class TimeUtils {
	/**
	 * 转换Timestamp为字符串
	 * 
	 * @return
	 */
	public static String getTimestamp() {
		return (new Timestamp(System.currentTimeMillis())).toString();
	}

	public static Timestamp getTimestamp(int hour) {

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		Timestamp time = new Timestamp(System.currentTimeMillis());
		time.setTime(cal.getTimeInMillis());
		return time;
	}

	/**
	 * 组织时间
	 * 
	 * @param time
	 * @return
	 */
	public static String setTime(String time) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		String po = calendar.get(Calendar.YEAR) + "-0"
				+ (calendar.get(Calendar.MONTH) + 1) + "-"
				+ calendar.get(Calendar.DATE) + " ";
		return (po + time);
	}

	/**
	 * 将传入的Timestamp 转换为字符串
	 * 
	 * @param timestamp
	 * @return String 的字符串
	 */
	public static String getTime(Timestamp timestamp) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = format.format(timestamp.getTime());

		return date;
	}

	/**
	 * 将传入的Timestamp 转换为字符串
	 * 
	 * @param timestamp
	 * @return String 的字符串
	 */
	public static String getTime(Timestamp timestamp, String fmat) {

		SimpleDateFormat format = new SimpleDateFormat(fmat);
		String date = format.format(timestamp.getTime());

		return date;
	}

	/**
	 * 根据输入的值计算出下次时间
	 * 
	 * @param timeValue
	 * @return
	 */
	public static String getNextTime(long timeValue) {

		long time = new Timestamp(System.currentTimeMillis()).getTime();
		long tmp = time + timeValue * 60 * 60 * 1000;

		return fromLongToDateString(tmp);
	}

	/**
	 * 获取yi天时间
	 * 
	 * @param timestamp
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(Timestamp str) throws ParseException {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String st = format.format(str);
		Date date = format.parse(st);
		return date;
	}

	/**
	 * 获取yi天时间
	 * 
	 * @param timestamp
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateTime(Timestamp str) throws ParseException {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String st = format.format(str);
		Date date = format.parse(st);
		return date;
	}

	/**
	 * 将传入的时间字符串 转换为 Timestamp
	 * 
	 * String 的字符串
	 * 
	 * @return Timestamp
	 */
	public static Timestamp getTimestamp(String dateString) {
		if(dateString==null) return null;
		if("".equals(dateString)) return null;
		SimpleDateFormat outsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {
			String datet1 = outsdf.format(outsdf.parse(dateString));
			Date d2 = outsdf.parse(datet1);
			Timestamp dateTime = new Timestamp(d2.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
			return dateTime;
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 此方法计算时间毫秒
	 * 
	 * @param inVal
	 * @return
	 */
	public static long fromDateStringToLong(String inVal) {
		Date date = null; // 定义时间类型
		SimpleDateFormat inputFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		try {
			// String st = inputFormat.format(inVal);
			date = inputFormat.parse(inVal); // 将字符型转换成日期型
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime();// 返回毫秒数
	}

	/**
	 * 将毫秒数换算成String
	 * 
	 * @param ms
	 * @return
	 */
	public static String fromLongToDateString(long ms) {
		SimpleDateFormat inputFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return inputFormat.format(ms);

	}

	/**
	 * 判断获取玩家最后一次登出时间和目前的时间的时间差是否大于30天
	 * 
	 * @param currTime
	 * @param logoutTime
	 * @return
	 */
	public static boolean sumPlayerOnlineTime(long currTime, long logoutTime) {//

		// 计算玩家没上线时间，最后将时间转化为以天为单位
		float notOnlineTime = (float) (currTime - logoutTime)
				/ (1000 * 3600 * 24);//

		if (notOnlineTime >= 15) {
			return true;
		}
		return false;
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static String getCurrTime() {
		SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return simpleDateTimeFormat.format(System.currentTimeMillis());
	}

	/**
	 * 字符串转为时间格式
	 * 
	 * @param str
	 * @return
	 * @throws ParseException
	 */
	public static String getStrTime(String str) throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.CHINESE);
		String date = format.format(format.parse(str));

		return date;
	}

	/**
	 * 获取当前时间
	 */
	public static String getTime() {
		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS",
				Locale.US);
		return df.format(date);
	}

	/**
	 * 获取格式化后的时间
	 */
	public static String getTime(Date date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.US);
		return df.format(date);
	}

	/**
	 * 获取格式化后的时间
	 */
	public static String getTime(Date date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format, Locale.US);
		return df.format(date);
	}

	/**
	 * 根据时间格式获取当前时间
	 */
	public static String getTime(String format) {
		Date date = Calendar.getInstance().getTime();
		SimpleDateFormat df = new SimpleDateFormat(format, Locale.US);
		return df.format(date);
	}

	/**
	 * 比较两时间的大小
	 * 
	 * @param time1
	 *            时间1
	 * @param time2
	 *            时间2
	 * @return 0：time1相等time2；<br>
	 *         1：time1大于time2；<br>
	 *         -1：time1小于time2；<br>
	 */
	public static int isDateBefore(String time1, String time2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.US);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(time1));
			c2.setTime(df.parse(time2));
		} catch (ParseException e) {
			return 0;
		}
		int result = c1.compareTo(c2);
		return result;
	}

	/**
	 * 本周一
	 * 
	 * @return the thisMonday
	 */
	public static Date getThisMonday() {
		Calendar cal = Calendar.getInstance();
		// 默认时，每周第一天为星期一需要更改一下
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String temp = sdf.format(cal.getTime());
		try {
			Date first = new Date(sdf.parse(temp + " 00:00:00").getTime());
			return first;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 本月一号
	 * 
	 * @return the thisJanuary
	 */
	public static Date getThisJanuary() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String temp = sdf.format(cal.getTime());
		try {
			Date thisJanuary = new Date(sdf.parse(temp + "-01 00:00:00")
					.getTime());
			return thisJanuary;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date getSomeDate(int i){
		Calendar c = Calendar.getInstance(); 
		c.add(Calendar.DATE, i);
		return c.getTime(); 
	}
	
	/**
	 * 时间相加
	 * 
	 * @param date
	 * @return
	 */
	public static Date plus(int date) {
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(c.getTime());
			c.add(Calendar.DATE, date);
			return c.getTime();
		} catch (Exception e) {
			Logger.getLogger(TimeUtils.class.getName()).log(Level.INFO, TimeUtils.class.getName(), "时间格式有误,请检查");
		}
		return null;
	}
	
	public static Date plus(String times, int date) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.setTime(format.parse(times));
//			Logger.getLogger(TimeUtils.class.getName()).log(Level.INFO, TimeUtils.class.getName(), "原来的时间:" + format.format(c.getTime()));
			c.add(Calendar.DATE, date);
			return c.getTime();
		} catch (ParseException e) {
			Logger.getLogger(TimeUtils.class.getName()).log(Level.INFO, TimeUtils.class.getName(), "时间格式有误,请检查");
			e.printStackTrace();
		}
		return null;
	}
}
